﻿@using Smartstore.Collections
@using Smartstore.Web.Rendering.Menus

@model MenuModel

@{
    //Layout = "";

    // targetNode is the node (with children) which was clicked. Means: we gonna navigate to its children.
    var targetNode = Model.SelectedNode ?? Model.Root;
    if (targetNode.IsLeaf && !targetNode.IsRoot)
    {
        targetNode = targetNode.Parent;
    }

    // currentNode represents the current page.
    var currentNode = ViewBag.CurrentNode as TreeNode<MenuItem>;
}

<div class="ocm-menu" data-depth="@targetNode.Depth">
    <div sm-if="targetNode.Depth > 0" class="ocm-menu-header ocm-back">
        <a href="@(targetNode.Parent.Value.GenerateUrl(this.Url) ?? "#")" class="ocm-link btn btn-secondary btn-icon btn-sm rounded-circle" data-id="@(targetNode.Parent.IsRoot ? 0 : targetNode.Parent.Id)" data-ajax="true">
            <i class="fa fa-arrow-left"></i>
        </a>
        <h5 class="ocm-title text-truncate" sm-language-attributes-for="WorkContext.WorkingLanguage">@targetNode.Value.Text</h5>
    </div>
    
    <ul class="ocm-nav list-unstyled">
        @if (targetNode.Depth > 0)
        {
            <li sm-if="!targetNode.Value.IsVoid()" class="show-current-node ocm-item">
                <a href="@targetNode.Value.GenerateUrl(this.Url)" class="ocm-link" data-id="@targetNode.Id" data-ajax="false">
                    @T("Offcanvas.Menu.ShowCurrentCat", targetNode.Value.Text)
                </a>
            </li>
        }

        @foreach (var node in targetNode.Children)
        {
            var item = node.Value;

            if (!item.Visible || item.IsGroupHeader)
            {
                continue;
            }

            // LI attributes
            var cssClass = "ocm-item";
            if (node.HasChildren)
            {
                cssClass += " has-children";
            }
            if (node == currentNode)
            {
                cssClass += " selected";
            }

            var attrs = new AttributeDictionary();
            attrs.Merge(item.HtmlAttributes);
            attrs.PrependCssClass(cssClass);

            // A attributes
            var linkAttrs = new AttributeDictionary();
            linkAttrs.Merge(item.LinkHtmlAttributes);
            if (node.Id != null)
            {
                linkAttrs.Add("data-id", node.Id.ToString());
            }
            linkAttrs.PrependCssClass("ocm-link has-count");

            if (node.HasChildren)
            {
                linkAttrs.Add("data-ajax", "true");
            }

            if (item.ElementsCount > 0)
            {
                linkAttrs.Add("data-count", item.ElementsCount.ToString());
            }

            <li attrs="attrs">
                <a href="@item.GenerateUrl(this.Url)" attrs="linkAttrs">
                    <span sm-language-attributes-for="WorkContext.WorkingLanguage">@item.Text</span>
                </a>
            </li>
        }
    </ul>
</div>